﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FirstFloor.ModernUI.Windows;
using FirstFloor.ModernUI.Windows.Controls;
using Lephone.Data;
using Lephone.Data.Definition;

namespace SimlvClient.Pages
{
    /// <summary>
    /// Interaction logic for Newslist.xaml
    /// </summary>
    public partial class Newslist : UserControl, IContent
    {



        public Newslist()
        {
            InitializeComponent();
        }

        public int CateId { get; set; }
        public long IsSimple { get; set; }
        public void GetGridData(int CateId)
        {
            try
            {
                IsSimple = 0;
                if (CateId == 0)
                {
                    IsSimple = 1;
                    BtnAdd.Visibility = Visibility.Collapsed;
                    BtnDelete.Visibility = Visibility.Collapsed;
                }
                else
                {
                    BtnAdd.Visibility = Visibility.Visible;
                    BtnDelete.Visibility = Visibility.Visible;
                }

                var NewsList = DbEntry.From<SimlvClient.Model.News>().Where(o => o.CateId == CateId && o.IsSimple == IsSimple).OrderByDescending(o => o.Id).Select().ToList();
                gridNews.ItemsSource = NewsList;
            }
            catch (Exception ex)
            {
            }
        }

        #region command
        public void OnFragmentNavigation(FirstFloor.ModernUI.Windows.Navigation.FragmentNavigationEventArgs e)
        {
            CateId = int.Parse(e.Fragment);
            GetGridData(int.Parse(e.Fragment));
        }

        public void OnNavigatedFrom(FirstFloor.ModernUI.Windows.Navigation.NavigationEventArgs e)
        {
        }

        public void OnNavigatedTo(FirstFloor.ModernUI.Windows.Navigation.NavigationEventArgs e)
        {
        }

        public void OnNavigatingFrom(FirstFloor.ModernUI.Windows.Navigation.NavigatingCancelEventArgs e)
        {
        }
        #region button click
        private void AddCommand(object sender, RoutedEventArgs e)
        {
            Model.News NewsModel = new Model.News { CateId = CateId, From = "", ModifyTime = Date.Now.ToString(), NContent = "请输入信息", Title = "请输入信息", IsSimple = IsSimple };
            NewsDetail NewPage = new NewsDetail(NewsModel);
            NewPage.Height = 600;
            NewPage.Width = 800;
            if (NewPage.ShowDialog().Value)
            {
                GetGridData(int.Parse(NewsModel.CateId.ToString()));
            }
            else
            {
                GetGridData(int.Parse(NewsModel.CateId.ToString()));
            }
        }

        private void EditCommand(object sender, RoutedEventArgs e)
        {
            var SelectModel = gridNews.SelectedItem as Model.News;
            if (SelectModel != null)
            {
                NewsDetail NewPage = new NewsDetail(SelectModel);
                NewPage.Height = 600;
                NewPage.Width = 800;
                if (NewPage.ShowDialog().Value)
                {
                    GetGridData(int.Parse(SelectModel.CateId.ToString()));
                }
                else
                {
                    GetGridData(int.Parse(SelectModel.CateId.ToString()));
                }
            }


        }
        private void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (gridNews.SelectedItem != null)
            {
                SelectModel = gridNews.SelectedItem as Model.News;
            }
            Thread thread = new Thread(new ThreadStart(DeletetoServer));
            thread.Start();


        }

        public Model.News SelectModel;

        private void DeletetoServer()
        {
            Dispatcher.Invoke(new Action(() =>
            {
                this.IsBusy.Visibility = Visibility.Visible;
                this.IsBusy.IsIndeterminate = true;
            }));
            string ftpServerIP = ConfigurationManager.AppSettings["ftpServerIP"];
            string ftpUserID = ConfigurationManager.AppSettings["ftpUserID"]; ;
            string ftpPassword = ConfigurationManager.AppSettings["ftpPassword"];
            string ImageFolder = ConfigurationManager.AppSettings["NewsImage"];
            if (ImageFolder != "")
            {
                ImageFolder = ImageFolder + "/";
            }
            FTPHelper fhelper = new FTPHelper(ftpServerIP, ftpUserID, ftpPassword);
            if (SelectModel != null)
            {
                if (SelectModel != null)
                {
                    foreach (var ImgModel in SelectModel.NewsImg)
                    {
                        fhelper.DeleteFileName(ImgModel.ImageUrl.Replace(ConfigurationManager.AppSettings["website"] + ImageFolder, ""));
                        ImgModel.Delete();
                    }
                    SelectModel.Delete();
                }
            }
            Dispatcher.Invoke(new Action(() =>
            {

                GetGridData(int.Parse(SelectModel.CateId.ToString()));
                this.IsBusy.IsIndeterminate = false;
                this.IsBusy.Visibility = Visibility.Collapsed;
                ModernDialog.ShowMessageText("删除成功！");
            }));
        }
        #endregion
        #endregion


        //DeleteCommand
    }
}
